package hanlder

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"order-pro/cache"
	"order-pro/models"
	"order-pro/pkg"
	"strconv"
	"time"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	var user models.User
	redis_key := "user_login_" + username
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if cacheData >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "错误超过三次，禁止登陆",
		})
		return
	}
	err := models.DB.Where("username = ?", username).First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码错误",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码错误",
		})
		return
	}
	if user.Password != pkg.Md5(password) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if cacheData == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "封禁两小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": user,
	})
	return
}
func Add(c *gin.Context) {
	uid, _ := strconv.Atoi(c.PostForm("uid"))
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	if uid == 0 || rid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.UserRole{
		Uid: uid,
		Rid: rid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 500,
		"msg":  "添加成功",
	})
	return
}
func URList(c *gin.Context) {
	uid, _ := strconv.Atoi(c.PostForm("uid"))

	var user models.UserRole
	redis_key := "UserRole_List_" + strconv.Itoa(uid)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Table("user_roles").
			Joins("LEFT JOIN roles ON user_roles.uid = roles.id").
			Where("user_roles.uid = ?", uid).
			Find(&user)
		jsonData, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &user)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": user,
	})
	return
}
func RoleOrderList(c *gin.Context) {
	id := c.Query("id")

	var user models.RoleOrderList
	redis_key := "UserRole_List_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Table("orders").
			Select(
				"orders.id AS OrderId",
				"orders.order_name AS OrderName",
				"orders.order_title AS OrderTitle",
				"roles.role_name AS RoleName",
				"roles.role_title AS RoleTitle",
			).
			Joins("LEFT JOIN roles ON roles.id = orders.rid").
			Find(&user)
		jsonData, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &user)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": user,
	})
	return
}
func NAdd(c *gin.Context) {
	num_name := c.PostForm("num_name")
	num_phone := c.PostForm("num_phone")
	if num_name == "" || num_phone == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Num{
		NumName:  num_name,
		NumPhone: num_phone,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func DingList(c *gin.Context) {
	id := c.Query("id")
	var user models.Ding
	redis_key := "Ding_List_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Where("id = ?", id).
			Find(&user)
		jsonData, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &user)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": user,
	})
	return
}
func NumDingList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	var user []models.Ding
	redis_key := "num_ding_page" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Offset(offset).
			Limit(size).
			Find(&user)
		jsonData, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &user)
	}
	var total int64
	models.DB.Model(models.NumDingList{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "列表展示成功",
		"data":  user,
		"total": total,
	})
	return
}
func DingsList(c *gin.Context) {
	did := c.Query("did")
	var user models.Ding
	redis_key := "Ding_List_" + did
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Table("dings").
			Select(
				"nums.id AS Nid",
				"nums.num_name AS NumName",
				"nums.num_phone AS NumPhone",
				"dings.ding_name AS DingName",
				"dings.ding_content AS DingContent",
			).
			Joins("LEFT JOIN nums ON nums.id = dings.nid").
			Where("dings.id = ?", did).
			Find(&user)
		jsonData, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &user)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": user,
	})
	return
}
func NumsDingList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	var user []models.NumDingList
	redis_key := "num_ding_page" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Table("dings").
			Select(
				"nums.id AS Nid",
				"nums.num_name AS NumName",
				"nums.num_phone AS NumPhone",
				"dings.ding_name AS DingName",
				"dings.ding_content AS DingContent",
			).
			Joins("LEFT JOIN nums ON nums.id = dings.nid").
			Offset(offset).
			Limit(size).
			Find(&user)
		jsonData, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &user)
	}
	var total int64
	models.DB.Model(models.NumDingList{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "列表展示成功",
		"data":  user,
		"total": total,
	})
	return
}
